import request from '@/utils/request'
import { otherFiltersItem } from '@/utils'

const domainCode = 'marketing'
const appCode = 'slm'

export function addPictureMaintain(data) {
  return request({
    url: '/api/v1/designPictureMaintain/addPictureMaintain',
    method: 'post',
    data
  })
}

export function saveConfirm(data) {
  return request({
    url: '/api/v1/shopDesign/pictureApproval',
    method: 'post',
    data
  })
}
export function domainUser(data) {
  return request({
    url: '/api/v1/user/domainUser/page',
    method: 'post',
    data
  })
}
// 业务账号
export function account(data) {
  return request({
    url: '/select-service/api/v1/account/page',
    method: 'post',
    data,
    headers: { 'Permission-App': 'slm', 'permission-code': `shopdesign` }
  })
}
// 客户
export function customer(data) {
  return request({
    url: '/select-service/api/v1/customer/page',
    method: 'post',
    data,
    headers: { 'Permission-App': 'slm', 'permission-code': `shopdesign` }
  })
}
/**
 * 根据字典类型批量查询字典值列表
 * @param {Array} dictTypeCodes 数据字典编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictByPatchType = async(dictTypeCodes) => {
  const res = await request({
    url: `/data-dict/api/v2/dictionary/patch/dictTypeCode`,
    method: 'post',
    params: { appCode, domainCode },
    data: dictTypeCodes
  })
  return res && res.data
}
export function pictureApproval(data) {
  return request({
    url: '/api/v1/designPictureApproval/pictureApproval',
    method: 'put',
    data
  })
}
export function drawingIssuance(data) {
  return request({
    url: '/api/v1/shopDesignPicture/drawingIssuance',
    method: 'post',
    data
  })
}
export function extension(data) {
  return request({
    url: '/api/v1/shopDesign/extension',
    method: 'post',
    data
  })
}
/**
 * 根据所属模块获取文件列表
 * @returns
 */
export function postConfigAttachmentTemplate() {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('belongModel', 'SHOP_DESIGN', '=')]))
  return request({
    url: '/api/v1/configAttachmentTemplate/page',
    method: 'POST',
    data
  })
}
/**
 * 获取异常类型
 * @returns
 */
export function getConfigExceptionHandle(typeCode) {
  const data = new FormData()
  const filtersRaw = []
  filtersRaw.push(otherFiltersItem('exceptionType', typeCode, '='))
  filtersRaw.push(otherFiltersItem('enable', 1, '='))
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/api/v1/configExceptionHandle/page',
    method: 'POST',
    data
  }).then(res => {
    const data = res.data.records[0]
    return data.id
  })
}

// 根据方案id获取系列表格详情
export function getSeriesTableById(id) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([
    otherFiltersItem('configProgramId', id, '='),
    otherFiltersItem('dr', '0', '='),
    otherFiltersItem('enable', '1', '=')]))
  return request({
    url: `/api/v1/configProgramSeries/page`,
    method: 'post',
    data
  })
}
// 获取套房-橱窗类列表
// spaceName: "产品类橱窗"
export function getShowCaseList() {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([
    otherFiltersItem('spaceName', '产品类橱窗', '=')]))
  return request({
    url: `/api/v1/configProgramProduct/baseAndSeries`,
    method: 'post',
    data
  })
}
// 根据方案id获取系列详情
export function getSeriesDataById(areaCode, programId) {
  const data = new FormData()
  if (!programId) {
    data.append('filtersRaw', JSON.stringify([
      otherFiltersItem('areaCode', areaCode, '='),
      otherFiltersItem('dr', '0', '='),
      otherFiltersItem('enable', '1', '=')]))
  } else {
    data.append('filtersRaw', JSON.stringify([
      otherFiltersItem('areaCode', areaCode, '='),
      otherFiltersItem('configProgramId', programId, '='),
      otherFiltersItem('dr', '0', '='),
      otherFiltersItem('enable', '1', '=')]))
  }
  return request({
    url: `/api/v1/configProgramProduct/baseAndSeries`,
    method: 'post',
    data
  })
}

// 根据返回的id获取方案详情
export function getProgramById(id, selectId) {
  return request({
    url: `/api/v1/shopDesign/getShopDesign/${id}/${selectId || undefined}/seriesInfos`,
    method: 'get'
  })
}
/**
 *
 * @param {id} 设计id
 * @returns
 */
export function getSeriesDetailById(id) {
  return request({
    url: `/api/v1/shopDesign/getShopDesign/${id}/seriesInfo`,
    method: 'get'
  })
}

// 保存方案详情
export function saveDesignAreaSeries(data) {
  return request({
    url: `/api/v1/shopDesign/addDesignAreaSeries`,
    method: 'post',
    data
  })
}

// 根据constCode获取测绘信息
export function getMapareaByShop(code) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([
    otherFiltersItem('constCode', code, '=')]))
  return request({
    url: `/api/v1/shopSurvey/queryByTerm`,
    method: 'post',
    data
  })
}

// 根据详情列表所有数据
export function getPruductAllData(id) {
  const data = new FormData()
  data.append('sort', 'isMustUp')
  data.append('order', 'desc')
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([
    otherFiltersItem('enable', '1', '='),
    otherFiltersItem('dr', '0', '='),
    otherFiltersItem('configProgramProductId', id, '=')
  ]))
  return request({
    url: `/api/v1/configProgramProductSeries/page`,
    method: 'post',
    data
  })
}

// 获取区域占比
export function areaRatio(id) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([
    otherFiltersItem('configAreaRatioId', id, '=')]))
  return request({
    url: `/api/v1/configAreaRatioDetail/page`,
    method: 'post',
    data
  })
}

// 获取区域适用面积配置
export function getRatioConfigPage(area) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([
    otherFiltersItem('suitMaxarea', area, '>='),
    otherFiltersItem('enable', 1, '='),
    otherFiltersItem('dr', 0, '='),
    otherFiltersItem('suitMinarea', area, '<=')
  ]))
  return request({
    url: `/api/v1/configAreaRatio/page`,
    method: 'post',
    data
  })
}
// 获取方案配置id获取区域
export function queryAreaByProCode(id) {
  const data = new FormData()
  data.append('filtersRaw', JSON.stringify([
    otherFiltersItem('configProgramId', id, '=')]))
  return request({
    url: `/api/v1/configProgramDetail/page`,
    method: 'post',
    data
  })
}
/**
 * 根据方案获取区域后，再对比区域配置列表，此接口返回区域配置列表启用数据
 * @param {*}
 * @returns
 */
export function queryEnableArea() {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('enable', 1, '=')]))
  return request({
    url: `/api/v1/configArea/page `,
    method: 'post',
    data
  })
}

/**
 * 终止仅有门头的数据
 * @param {data} 终止原因
 * @returns
 */
export function termination(data) {
  return request({
    url: `/api/v1/shopDesign/termination`,
    method: 'post',
    data
  })
}

/**
 * 修改门店等级
 * @param {data}
 * @returns
 */
export function editShopLevelCode(data) {
  return request({
    url: `/api/v1/shopDesign`,
    method: 'put',
    data
  })
}

/**
 * 定制报图导出
 * @param {object} data
 * @returns
 */
export function getCustomizeFile(data) {
  return request({
    url: `/api/v1/shopDesign/exportPicReportSeries`,
    method: 'post',
    responseType: 'blob',
    data
  })
}
